package Staff;

import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import model.modelTable;

/**
 *
 * @author langtustupid
 */
public class Staff extends javax.swing.JInternalFrame {

    Connection Con;
    Statement Sta;
    ResultSet Res;

    public Staff() {
        initComponents();
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Con = DataConnect.connectDB();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Search = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_Staff = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnAdd = new javax.swing.JMenu();
        mnDelete = new javax.swing.JMenu();
        mnShow = new javax.swing.JMenu();
        mnclose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Staff Management");
        setPreferredSize(new java.awt.Dimension(1056, 481));

        jPanel2.setPreferredSize(new java.awt.Dimension(1000, 583));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Staff_Magament.png"))); // NOI18N

        jLabel2.setText("Staff Name :");

        jTextField_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField_SearchKeyReleased(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Search.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(291, 291, 291)
                .addComponent(jLabel1)
                .addContainerGap(235, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(347, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(34, 34, 34)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(387, 387, 387))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3))
                    .addComponent(jLabel2)))
        );

        jTable_Staff.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable_Staff.setRowHeight(20);
        jTable_Staff.setSelectionBackground(new java.awt.Color(255, 153, 255));
        jScrollPane1.setViewportView(jTable_Staff);

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        mnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/add.gif"))); // NOI18N
        mnAdd.setText(" Add Staff ");
        mnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnAddMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnAdd);

        mnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/delete.png"))); // NOI18N
        mnDelete.setText("Delete Staff ");
        mnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDeleteMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDelete);

        mnShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/star.gif"))); // NOI18N
        mnShow.setText("Show Staff");
        mnShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShow);

        mnclose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        mnclose.setText("Close");
        mnclose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mncloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnclose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1040, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1040, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                .addGap(32, 32, 32)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDeleteMouseClicked
        if (jTable_Staff.getSelectedRow() != -1) {
            int remove = JOptionPane.showConfirmDialog(this, "Are you sure want delete this Staff !");

            if (remove == 0) {
                int index = Integer.parseInt(jTable_Staff.getValueAt(jTable_Staff.getSelectedRow(), 0).toString());
                try {

                    String sql_del = "Update Staff set Staff_status = 0 where Staff_Id=" + index;
                    Sta.executeUpdate(sql_del);
                    Res = Sta.executeQuery("Select * from Staff where Staff_status = 1");
                    jTable_Staff.setModel(new modelTable(Res));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                JOptionPane.showMessageDialog(this, "Delete Staff success ! ");
            }

        } else {
            JOptionPane.showMessageDialog(null, "You should choose the Staff want to delete !");
        }
    }//GEN-LAST:event_mnDeleteMouseClicked

    private void jTextField_SearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SearchKeyReleased
        try {
            Res = Sta.executeQuery("Select *from Staff where Staff_status = 1 and Staff_Name like '%" + jTextField_Search.getText().trim() + "%'");
            jTable_Staff.setModel(new modelTable(Res));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_jTextField_SearchKeyReleased

    private void mnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnAddMouseClicked
        new Add_Staff().setVisible(true);
    }//GEN-LAST:event_mnAddMouseClicked

    private void mnShowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowMouseClicked
        try {
            Sta = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String sql = "Select Staff_Id,Staff_Name,Staff_Username,Staff_Sex,Staff_age,Staff_Phone,Staff_Address,Staff_Email,Staff_Permission from Staff where Staff_status = 1";
            Res = Sta.executeQuery(sql);
            jTable_Staff.setModel(new modelTable(Res));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_mnShowMouseClicked

private void mncloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mncloseMouseClicked
// TODO add your handling code here:
      int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
}//GEN-LAST:event_mncloseMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Staff().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_Staff;
    private javax.swing.JTextField jTextField_Search;
    private javax.swing.JMenu mnAdd;
    private javax.swing.JMenu mnDelete;
    private javax.swing.JMenu mnShow;
    private javax.swing.JMenu mnclose;
    // End of variables declaration//GEN-END:variables
}
